void drawPoint(Point p)
{
  point(p.x, p.y);
}

void drawTriangle(Triangle t)
{
  triangle(t.a.x, t.a.y, t.b.x, t.b.y, t.c.x, t.c.y);
}

void fillTriangle(Triangle t)
{
  for(int x = 0; x < width; x++)
    for(int y = 0; y < height; y++)
    {
      Point p = new Point(x, y);
      if(t.inside(p))
      {
        drawPoint(p);
      }
    }
}

Point randomPoint()
{
  float x = random(0, width);
  float y = random(0, height);
  return new Point(x, y);
}

class Triangle{
  Point a,b,c;
  
  Line AB, BC, AC;
  
  int condAB, condBC, condAC;
  
  public Triangle(Point a, Point b, Point c)
  {
    this.a = a;
    this.b = b;
    this.c = c;
    
    this.AB = new Line(a, b);
    this.BC = new Line(b, c);
    this.AC = new Line(a, c);
    // Guardar el signo de la desigualdad
    // para cada recta
    this.condAB = this.AB.evaluate(c);
    this.condBC = this.BC.evaluate(a);
    this.condAC = this.AC.evaluate(b);
  }
  
  boolean inside(Point p) 
  {
    if(this.AB.evaluate(p) == this.condAB && this.BC.evaluate(p) == this.condBC && this.AC.evaluate(p) == this.condAC)
      return true;
    else
      return false;
  }
};

class Point{
  float x, y;
  public Point(float x, float y)
  {
    this.x = x;
    this.y = y;
  }
};

class Line{
  Point A, B;
  
  float a, b, c;
  
  public Line(Point A, Point B)
  {
    this.A = A;
    this.B = B;
    float p = (B.y-A.y) / (B.x-A.x);
    this.a = p;
    this.b = -1;
    this.c = A.y - p * A.x;
  }
  
  int evaluate(Point p)
  {
    float result = (this.a * p.x + this.b * p.y + this.c);
    if(result < 0)
      return -1;
    else if(result > 0)
      return 1;
    else
      return 0;
  }
}

void setup()
{
  size(640, 480);
  noFill();
  Triangle t = new Triangle(randomPoint(), randomPoint(), randomPoint());
  fillTriangle(t);
}


